<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_FLIER_PRICE.php' );
require_once( '../system/mdao/Class_M_COST_FLIER_PRICE_AREA.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_QR,$login['staff_auth']);

$dosearch = FALSE ;



$forms = $_SESSION['MSEARCH']['COST_FLIER_PRICE'] ;
$year4 = $forms["year4"] ;

$frmdatas = $_POST;


//行列交換
foreach($frmdatas as $k => $v)
 {
	 foreach($v as $kk =>$vv){
	     $data[$kk][$k]= $vv;
	 }
 }

if ( $frmdatas['hsort'] == '1' ) {
	$dao = new Class_M_COST_FLIER_PRICE;
	$p_cd= "shop_cd";
	$p_caption= "店舗";
}else {
	$dao = new Class_M_COST_FLIER_PRICE_AREA;
	$p_cd= "pattern_cd"; 
	$p_caption= "パターン";
}
//echo $p_caption."*****";
$hdata = array();
$ddata = array();

foreach($data as $k => $v) {
	if (!empty($v["b2"])) {
		$hdata[] = array($p_cd=> $v["shop_cd"], "size"=> "B2", "price"=> $v["b2"], "year4"=> $year4);
	}
	
	if (!empty($v["b3"])) {
		$hdata[] = array($p_cd=> $v["shop_cd"], "size"=> "B3", "price"=> $v["b3"], "year4"=> $year4);
	}
	
	if (!empty($v["b4"])) {
		$hdata[] = array($p_cd=> $v["shop_cd"], "size"=> "B4", "price"=> $v["b4"], "year4"=> $year4);
	}

	
}

$msg = "チェックエラー";
$errmsg =  "";

$err     = new Class_ERROR("","");
$chk     = $dao->get_checks();

$errfields = array("shop_cd" => "{$p_caption}コード","price" => "価格", "year4"=> "年度"); 

foreach($hdata as $k => $v) {
	//ヘッダチェック
	foreach ($chk as $kcol => $chkval) {
		$errmsg  = $err->check( $v[$kcol], $chkval );
		
		if( !($err->clear) ){
			$errmsg = "{$p_caption}:".$v[$p_cd]."の".$errfields[$kcol]."は". $errmsg ;
			break;
		}
	}
	if( $errmsg ){
		break;
	}
/*
//明細チェック
	foreach ($ddata[$v["ins_shop_shop_cd"]] as $key =>$vv) {

		$errmsg  = $err->check( $vv["pnums"], array( "NUM" ) );

		if( !($err->clear) ){
			$errmsg = $patttenFields[$vv["pattern_cd"]].":". $errmsg ;
			break;
		}
	}
	*/
	if( $errmsg ){
		break;
	}
}


if (empty($errmsg)) {
	try {
		$dao->begin_trans();
		$executeflg = TRUE;
		
		foreach($hdata as $k => $v) {
			$shop_pattern_cd = $v[$p_cd];
			$size = $v['size'];
			$where = " year4 = '{$year4}' and {$p_cd} = '{$shop_pattern_cd}' and size = '{$size}'  " ;
			if ($dao->exits($where)) {
				if (!$dao->editbywhere($where, $v)) {
					$executeflg = FALSE;
					break;
				}
			} else {
				if (!$dao->add($v)){
					$executeflg = FALSE;
					break;
				}
			}
		}
		if  ($executeflg) {
			$dao->commit_trans();
			$msg =  '更新が完了しました。';
		} else {
			$dao->rollback_trans();
			$msg =  '更新が失敗しました。';
		}

		
	} catch (Exception $e) {
		$dao->rollback_trans();
		$msg = 'ＤＢエラーで失敗しました。';
	}
}

 $smarty->assign('sorts', $aoki_flier_price_sort);  
if( isset($forms) )     $smarty->assign('forms', $forms);
if( isset($data) )      $smarty->assign('data', $data);
$page["cnt"] = count($data);  
$smarty->assign('page', $page);

$smarty->assign('year4',  Class_PWD::get_years());
$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );

$smarty->assign('msg', $msg );
$smarty->assign('errmsg', $errmsg );

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_flier_price.html');

?>